package org.jeecg.common.util;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import javassist.Modifier;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.sms.SendSmsDTO;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

/**

 * 动态设置获取类的属性值

 * @author muyuer 182443947@qq.com

 * @version 1.0

 * @date 2019-08-12 16:51

 */
@Slf4j
public class ObjectUtils {
    /**
     * 过滤不需要属性
     * @param field
     * @return
     */
    private static Boolean needFilterField(Field field){

        // 过滤静态属性
        if(Modifier.isStatic(field.getModifiers())){
            return true;
        }

        // 过滤transient 关键字修饰的属性
        if(Modifier.isTransient(field.getModifiers())){
            return true;
        }
        return false;
    }

    /**
     * 利用Java反射根据类的名称获取属性信息和父类的属性信息
     * @param obj
     * @return
     */
    public static Map getFieldList(Object obj) throws IllegalAccessException {

        Map fieldMap = new LinkedHashMap<>();
        Class<?> clazz = obj.getClass();

        Field[] fields = clazz.getDeclaredFields();
        for(Field field : fields){
            log.info(field.getName());
            if (needFilterField(field)) {
                continue;
            }
            fieldMap.put(field.getName(), getFieldValue(obj,field.getName()));
        }
        getParentField(obj, fieldMap);
        return fieldMap;
    }

    /**
     * 递归所有父类属性
     * @param fieldMap
     */

    private static void getParentField(Object obj, Map fieldMap){

        Class superClazz = obj.getClass().getSuperclass();

        if (superClazz != null) {
            Field[] superFields = superClazz.getDeclaredFields();
            for(Field field : superFields){
                if (needFilterField(field)) {
                    continue;
                }
                fieldMap.put(field.getName(), getFieldValue(obj,field.getName()));
            }
           // getParentField(superClazz, fieldMap);
        }
    }

    /**

     * 根据类获取属性信息和父类的属性信息

     * @param obj

     * @return

     */

    public static Map getMethodMap(Object obj) {

        Map methodMap = new LinkedHashMap<>();
        Class clazz = obj.getClass();
        Method[] methods = clazz.getMethods();
        for(Method method : methods){
            methodMap.put(method.getName(), method);
        }
        getParentMethod(clazz, methodMap);
        return methodMap;
    }

    /**

     * 递归所有父类方法

     * @param clazz

     * @param methodMap

     */

    private static void getParentMethod(Class clazz, Map methodMap){
        Class superClazz = clazz.getSuperclass();
        if (superClazz != null) {
            Method[] superMethods = superClazz.getMethods();
            for(Method field : superMethods){
                methodMap.put(field.getName(), field);
            }
            getParentMethod(superClazz, methodMap);
        }
    }

    /**
     * 根据属性名获取属性值
     * @param obj
     * @param fieldName
     * @return
     */
    public static Object getFieldValue(Object obj, String fieldName) {

        try {
            Map methodMap = getMethodMap(obj);
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = (Method)methodMap.get(getter);
            Object value = null;
            if (method != null){
                value = method.invoke(obj, new Object[] {});
            }
            return value;
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 设置属性值
     * @param obj
     * @param fieldName
     * @param value
     */
    public static void setFieldValue(Object obj, String fieldName, Object value) {

        try {
            Map fields = getFieldList(obj);
            Field f = (Field)fields.get(fieldName);
            if (f != null) {
                f.setAccessible(true);
                f.set(obj, value);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    public static void main(String[] args) throws IllegalAccessException {
        SendSmsDTO sendSmsDTO = new SendSmsDTO();
        sendSmsDTO.setPhoneNumber("18936676366");
        sendSmsDTO.setSign("sign");
       System.out.println(ObjectUtils.getFieldList(sendSmsDTO));

    }
}